﻿
Imports osi.root.delegates

Public Module _manual_disposer
    Public Function make_manual_disposer(Of T)(ByVal p As _do(Of T),
                                               Optional ByVal init As void = Nothing,
                                               Optional ByVal disposer As void(Of T) = Nothing) _
                                              As manual_disposer(Of T)
        Return New manual_disposer(Of T)(p, init, disposer)
    End Function

    Public Function make_manual_disposer(Of T)(ByVal p As T,
                                               Optional ByVal init As void = Nothing,
                                               Optional ByVal disposer As void(Of T) = Nothing) _
                                              As manual_disposer(Of T)
        Return New manual_disposer(Of T)(p, init, disposer)
    End Function

    Public Function regional_action(ByVal start As void,
                                    ByVal [end] As void) As manual_disposer(Of Int32)
        assert(Not start Is Nothing)
        assert(Not [end] Is Nothing)
        Return make_manual_disposer(0, start, Sub(ByRef x) [end]())
    End Function
End Module

Public Class manual_disposer(Of T)
    Inherits disposer(Of T)
    Implements IDisposable

    Public Sub New(ByVal p As _do(Of T),
                   Optional ByVal init As void = Nothing,
                   Optional ByVal disposer As void(Of T) = Nothing)
        MyBase.New(p, init, disposer)
    End Sub

    Public Sub New(ByVal p As T,
                   Optional ByVal init As void = Nothing,
                   Optional ByVal disposer As void(Of T) = Nothing)
        MyBase.New(p, init, disposer)
    End Sub

    Public Overloads Sub Dispose() Implements IDisposable.Dispose
        MyBase.dispose()
    End Sub
End Class
